package com.dhcc.bpm.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.modules.system.entity.UserDept;
import com.dhcc.bpm.modules.system.mapper.UserDeptMapper;
import com.dhcc.bpm.modules.system.service.IUserDeptService;
import com.dhcc.bpm.modules.system.vo.UserDeptVO;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserDeptServiceImpl
 * @Description 服务实现类
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Service
public class UserDeptServiceImpl extends ServiceImpl<UserDeptMapper, UserDept> implements IUserDeptService {

    /**
     * 自定义分页
     * @Author sjm
     * @Date 19:20 2020/2/21 0021
     * @param page
     * @param userDept
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.UserDeptVO>
     **/
	@Override
	public IPage<UserDeptVO> selectUserDeptPage(IPage<UserDeptVO> page, UserDeptVO userDept) {
		return page.setRecords(baseMapper.selectUserDeptPage(page, userDept));
	}

	/**
	 * 用户部门中间表
	 * @Author sjm
	 * @Date 19:20 2020/2/21 0021
	 * @param userId
	 * @param deptId
	 * @return boolean
	 **/
	@Override
	public boolean batchUserDept(Long userId, String deptId) {
        List<Long> deptIds = Func.toLongList(deptId);
        List<UserDept> list = new ArrayList<>();
        deptIds.forEach(dept->{
            UserDept userDept = new UserDept();
            userDept.setUserId(userId);
            userDept.setDeptId(dept);
            list.add(userDept);
        });
        return saveBatch(list);
	}

	/**
	 * 查询集合
	 * @Author sjm
	 * @Date 19:21 2020/2/21 0021
	 * @param userId
	 * @return java.util.List<com.dhcc.bpm.modules.system.entity.UserDept>
	 **/
    @Override
    public List<UserDept> selectDeptByUserId(Long userId) {
        return baseMapper.selectDeptByUserId(userId);
    }

    /**
     * 根据userId删除
     * @Author sjm
     * @Date 19:21 2020/2/21 0021
     * @param userId
     * @return boolean
     **/
    @Override
    public boolean removeByUserId(Long userId) {
        return baseMapper.removeByUserId(userId);
    }

}
